package com.wyh.security.gfive.service;



import com.wyh.domain.dto.gfive.LoginUser;
import com.wyh.domain.po.gfive.User;


import com.wyh.service.gfive.RoleService;
import com.wyh.service.gfive.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;


/*
* 用于权限验证
*
* */
@Service
public class UserDetailsServiceSecurityImpl implements UserDetailsService {

    @Autowired
    private UserService userService;

    @Autowired
    private PermissionsServiceSecurityImpl permissionsService;

    @Autowired
    private RoleService roleService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

      User user =  userService.findByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }


        return new LoginUser(roleService.selectRoleByUserId(user.getId()),permissionsService.getMenuPermission(user),user );
    }


}




